#! /usr/local/bin/python

# Web 2.0
# A simple CGI script to act as a proxy server for the blogging web service
# 
# USAGE: proxyBlog.cgi?method=<method name>&option=value&option=value...
#
# For example, proxyBlog.cgi?method=getPost&type=html&author=cs4380
#
# Note: fill in your own username and passwd!

import cgitb; cgitb.enable()
import cgi
import urllib
import urllib2
import sys

# get form variables (if desired)
form = cgi.FieldStorage()
id = form["id"].value
username = form["author"].value
passwd = form["password"].value

# your username and password for the blog service
blogurl  = 'http://www.cs.cityu.edu.hk/~cs4380/webservice/blog/deletePost.py'

# setup the password
password_manager = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_manager.add_password(None, 'http://www.cs.cityu.edu.hk/', username, passwd)
auth_handler = urllib2.HTTPBasicAuthHandler(password_manager)
opener = urllib2.build_opener(auth_handler)
urllib2.install_opener(opener)

# setup the parameters for calling the web service
paramslist = {
  'id': id
}

params = urllib.urlencode(paramslist)

# call the webservice
data = urllib2.urlopen(blogurl, params).read()

# print out the content header
print 'Content-type: text/html\n\n'

#print blogurl
#print '\n'
#print paramslist
#print '\n'

print data

